package com.qiqijuice.sales;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Layout;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

public class CategoryListView extends Composite {

	public static int EVT_CATEGORY_SELETED = 0x2000;
	
	static int MAX_CHOICE_PER_PAGE = 15;
	
	static int HORIZONTAL_PADDING = 10;
	static int X_GAP = 5;
	static int BUTTON_DIM = 150;
	
	boolean inResize = false;
	
	Composite content;
	Button all;
	Button pageUp;
	Button pageDown;
	
	int choicePerPage;
	int currentPage;
	Button[]  choices = new Button[MAX_CHOICE_PER_PAGE];
	
	public CategoryListView(Composite parent) {
		super(parent, SWT.NONE);
		
		MigLayout layout = new MigLayout("ins 0 20 0 20, fill","");
		super.setLayout(layout);
		
		all = new Button(this, SWT.PUSH);
		all.setText("All");
		all.setLayoutData("west, width 50, height 50");

		pageUp = new Button(this, SWT.PUSH);
		pageUp.setText("Page Up");
		pageUp.setLayoutData("west, height 50");
		pageUp.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				int old = currentPage;
				--currentPage;
				if (currentPage < 0)
					currentPage = 0;
				
				if (old != currentPage)
					updateChoices();
			}
			
		});
		pageDown = new Button(this, SWT.PUSH);
		pageDown.setText("Page Down");
		pageDown.setLayoutData("west, height 50");
		pageDown.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				int old = currentPage;
				++currentPage;
				
				ObjectContainer oc = Db4oContainerFactory.getDB();
				ObjectSet<ProductCategory> result = oc.query(ProductCategory.class);
				int total = result.size();
				
				if (currentPage >= total / choicePerPage)
					currentPage = total / choicePerPage - 1;
				
				if (currentPage < 0)
					currentPage = 0;
				
				if (old != currentPage)
					updateChoices();
			}
			
		});
		
		content = new Composite(this, SWT.NONE);
		//RowLayout conLayout = new RowLayout();
		MigLayout conLayout = new MigLayout("ins 0 0 0 0","");
		content.setLayout(conLayout);
		content.setLayoutData("center, grow");
		content.addControlListener(new ControlAdapter() {

			@Override
			public void controlResized(ControlEvent e) {
				contentResized();
			}
			
		});
		
		currentPage = 0;
		for(int i=0; i<MAX_CHOICE_PER_PAGE; ++i) {
			Button c = new Button(content, SWT.PUSH);
			c.setVisible(false);
			c.addSelectionListener(new SelectionAdapter() {

				@Override
				public void widgetSelected(SelectionEvent e) {
					Event ee = new Event();
					ee.data = e.widget.getData();
					notifyListeners(EVT_CATEGORY_SELETED, ee);
				}
				
			});
			choices[i] = c;
		}
	}

	void contentResized() {
		if (inResize)
			return;
		
		inResize = true;
		int width = content.getSize().x;
		choicePerPage = (width - 2 * HORIZONTAL_PADDING - X_GAP) / (BUTTON_DIM + X_GAP);
		if (choicePerPage > MAX_CHOICE_PER_PAGE)
			choicePerPage = MAX_CHOICE_PER_PAGE;
		
		System.out.println("Choice per page: " + choicePerPage);
		currentPage = 0;
		updateChoices();
		inResize = false;
	}

	void updateChoices() {
		
		ObjectContainer oc = Db4oContainerFactory.getDB();
		ObjectSet<ProductCategory> result = oc.query(ProductCategory.class);
		
		int start_idx = currentPage * choicePerPage;
		int cur_idx = 0;
		while(result.hasNext() && cur_idx < start_idx + choicePerPage) {
			ProductCategory pc = result.next();
			if (cur_idx >= start_idx) {
				choices[cur_idx-start_idx].setText(pc.getName());
				choices[cur_idx-start_idx].setVisible(true);
				//choice[cur_idx-start_idx].setSize(BUTTON_DIM, 50);
				choices[cur_idx-start_idx].setLayoutData(String.format("width %1$d:%1$d:%1$d, height 50", BUTTON_DIM));
				choices[cur_idx-start_idx].setData(pc);
			}
			
			++cur_idx;
		}
		
		for (; cur_idx < start_idx + choicePerPage; ++cur_idx) {
			choices[cur_idx-start_idx].setText("");
			choices[cur_idx-start_idx].setVisible(false);
			choices[cur_idx-start_idx].setData(null);
		}
	}

	@Override
	public void setLayout(Layout layout) {
		checkWidget();
	}
}